package cn.simon.exception;

import cn.simon.vo.RespBean;
import cn.simon.vo.RespBeanEnum;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/*@RestControllerAdvice*/
public class GlobalExceptionHandler{

    @ExceptionHandler(Exception.class)
    public RespBean ExceptionHandler(Exception e){
        if(e instanceof GlobalException){
            GlobalException ex = (GlobalException) e;
            return RespBean.error(ex.getRespBeanEnum());
        }else if(e instanceof BindException){
            BindException eb = (BindException) e;
            RespBean respBean = RespBean.error(RespBeanEnum.BIND_ERROR);
            respBean.setMessage("参数检验异常："+eb.getBindingResult().getAllErrors().get(0));
            return respBean;
        }
        return RespBean.error(RespBeanEnum.ERROR);
    }
}
